const sql = require('../sql/sqlConfig');
const express = require('express');
let router = express.Router();
const pagination = require('../util/page');

//查订单列表
router.get('/', function (req, res, next) {
    let countPageSql = "select count(`id`) as total from `order`";
    let page = req.query.page;
    let option = "select o.`id`, o.`create_time`, p.`name` as product, o.`num`, o.`total_price`, o.`address`, o.`order_payment_status`, u.`email` as consumer, u1.`email` as distributor from `order` as o, `product` as p, `user` as u, `user` as u1 where p.`id`=o.`product_id` and u.`id`=o.`user_id` and u1.`id`=o.`pid` limit ";
    pagination(countPageSql,page,option,function (result) {
        return res.send(result)
    });
});

//搜索
router.get('/search', function (req, res, next) {
    let orderId = req.query.search;
    let option = "select o.`id`, o.`create_time`, p.`name` as product, o.`num`, o.`total_price`, o.`address`, o.`order_payment_status`, u.`email` as consumer, u1.`email` as distributor from `order` as o, `product` as p, `user` as u, `user` as u1 where p.`id`=o.`product_id` and u.`id`=o.`user_id` and u1.`id`=o.`pid` and o.`id`="+orderId;
    sql.query(option, function (err, rows) {
        if (err) return res.send({errCode:-9999,message:'查询失败'+err});
        if(rows.length==0){
            return res.send({errCode:9,message:'查无此订单号的信息'});
        }
        res.send({errCode:0,message:'查询订单id成功',result:rows});
    })
});

module.exports = router;
